package com.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.entity.Collect;
import com.entity.User;
import com.service.collect.impl.CollectServiceImpl;
import com.servlet.BaseServlet;
import com.utils.ReturnResult;

/**
 * 收藏夹
 */
@WebServlet("/CollectServlet")
public class CollectServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	/**
	 * 添加到收藏夹
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ReturnResult addCollect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ReturnResult result = new ReturnResult();
		//获取商品id
		int id = Integer.parseInt(request.getParameter("id"));
		int type = Integer.parseInt(request.getParameter("type"));
		//获取session中的当前用户对象
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("easybuyUserLogin");
		//判断用户是否登录
		if(user == null) {
			result.returnNo("您未登录，请先登录！");
		}else {
			Collect collect = new CollectServiceImpl().finCollectUserCount(id);
			if(collect == null) {
				//收藏
				int addRow = new CollectServiceImpl().finAddCollect(user.getId(),id, type);
				if(addRow>0) {
					result.returnYes("收藏成功");
				}else {
					result.returnNo("收藏失败！");
				}
			}else {
				//删除收藏
				int removeRow = new CollectServiceImpl().finRemoveCollect(user.getId(), id);
				if(removeRow>0) {
					result.returnYes("成功取消收藏！");
				}else {
					result.returnNo("取消收藏失败！");
				}
			}
		}
		return result;
	}
	
	
	
	
	
	
	
	

	@Override
	public Class<?> getServletClass() {
		return CollectServlet.class;
	}


}
